//
//  UIColor+ZJ.swift
//  ZJSwiftKit
//
//  Created by 万孟超 on 2023/11/14.
//

import UIKit

extension ZJPrefix where Base: UIColor {
    
    /// 颜色的red值
    public var red: CGFloat {
        var r: CGFloat = 0
        base.getRed(&r, green: nil, blue: nil, alpha: nil)
        return r
    }
    
    /// 颜色的green值
    public var green: CGFloat {
        var g: CGFloat = 0
        base.getRed(nil, green: &g, blue: nil, alpha: nil)
        return g
    }
    
    /// 颜色的blue值
    public var blue: CGFloat {
        var b: CGFloat = 0
        base.getRed(nil, green: nil, blue: &b, alpha: nil)
        return b
    }
    
    /// 颜色的alpha值
    public var alpha: CGFloat {
        return base.cgColor.alpha
    }
    
    /// 生成颜色图片
    /// - Parameters:
    ///   - size: 大小
    ///   - cornerRadius: 圆角
    /// - Returns: 图片
    public func image(size: CGSize = CGSize(width: 1.0, height: 1.0), cornerRadius: CGFloat = 0) -> UIImage? {
        let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
        UIGraphicsBeginImageContext(size)
        let roundedRect = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
            
        let context:CGContext = UIGraphicsGetCurrentContext()!
        context.setFillColor(base.cgColor);
        roundedRect.fill()
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}
